package edu.nf.exam.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.dao.SubjectInfoDao;
import edu.nf.exam.dao.impl.SubjectInfoDaoImpl;
import edu.nf.exam.entity.SubjectInfo;
import edu.nf.exam.exception.DataAcessException;
import edu.nf.exam.service.SubjectService;

import java.util.List;

/**
 * @Author : QYM
 * @create 2021/6/17
 * 学科信息表--业务接口实现类
 */
public class SubjectServiceImpl implements SubjectService {
    /**
     * 添加学科
     * @param subjectInfo
     * @return
     */
    @Override
    public void checkAddSubject(SubjectInfo subjectInfo) {
        SubjectInfoDao dao = new SubjectInfoDaoImpl();
        try {
            dao.addSubject(subjectInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new DataAcessException("添加失败");
        }
    }

    /**
     * 删除学科
     * @param subjectId
     * @return
     */
    @Override
    public void checkDelSubject(Integer subjectId) {
        SubjectInfoDao dao = new SubjectInfoDaoImpl();
        try {
            dao.delSubject(subjectId);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new DataAcessException("删除失败");
        }
    }

    /**
     * 修改学科
     * @param subjectInfo
     * @return
     */
    @Override
    public void checkUpdateSubject(SubjectInfo subjectInfo) {
        SubjectInfoDao dao = new SubjectInfoDaoImpl();
        try {
            dao.updateSubject(subjectInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new DataAcessException("修改失败");
        }
    }

    /**
     * 模糊查询学科
     * @param levelName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SubjectInfo> checkQueryByLikeSubject(String levelName, Integer pageNum, Integer pageSize) {
        //通过dao查询返回分页数据集合
        List<SubjectInfo> list = new SubjectInfoDaoImpl().queryByLikeSubject(levelName,pageNum,pageSize);
        if (list==null){
            throw new DataAcessException("数据为空");
        }
        //封装为分页pageInfo对象
        PageInfo<SubjectInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据ID查询
     * @param subjectId
     */
    @Override
    public SubjectInfo checkQueryById(Integer subjectId) {
        SubjectInfoDao dao = new SubjectInfoDaoImpl();
        try {
            return dao.queryByIdSubject(subjectId);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new DataAcessException("根据ID查询失败");
        }
    }

    /**
     * 去重查询年级
     * @return
     */
    @Override
    public List<SubjectInfo> selectLevelNameByDistinct() {
        SubjectInfoDao dao=new SubjectInfoDaoImpl();
        List<SubjectInfo> list = dao.selectLevelNameByDistinct();
        return list;
    }

    /**
     * 多条件查询
     * @param subjectInfo
     * @return
     */
    @Override
    public List<SubjectInfo> selectByMore(SubjectInfo subjectInfo) {
        SubjectInfoDao dao=new SubjectInfoDaoImpl();
        List<SubjectInfo> list = dao.selectByMore(subjectInfo);
        return list;
    }

    /**
     * 根据学科名称和年级查询，服务于判断学科创编和修改是否唯一
     * @param subjectName
     * @param levelName
     */
    @Override
    public void checkSubject(String subjectName, String levelName) {
        SubjectInfoDao dao = new SubjectInfoDaoImpl();
        int count = dao.queryByNameSubject(subjectName, levelName);
        if(count != 0){
            throw new DataAcessException("此记录已存在");
        }
    }
}
